//
//  LookForPasswordViewController.m
//  PlayBall
//
//  Created by superman on 14-8-20.


#import "LookForPasswordViewController.h"
#import "JddDataParser.h"
#import "JddViewControllerManager.h"

@interface LookForPasswordViewController ()

@end

@implementation LookForPasswordViewController
@synthesize phoneNumTextfield;
@synthesize userNameTextfield;

SHARED_VIEW_CONTROLLER_IMPLEMENT(LookForPasswordViewController)


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [MobClick beginLogPageView:@"LookForPassword"];
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"LookForPassword"];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    customLab.backgroundColor = [UIColor clearColor];
    [customLab setText:@"找回密码"];
    customLab.textAlignment = NSTextAlignmentCenter;
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.font = [UIFont systemFontOfSize:19];
    self.navigationItem.titleView = customLab;
    //self.navigationItem.hidesBackButton = YES;
    
    self.userNameTextfield.delegate = self;
    self.phoneNumTextfield.delegate = self;
    self.userNameTextfield.text = self.userName;
    self.userNameTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.phoneNumTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.phoneNumTextfield.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    
    UITapGestureRecognizer *oneTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
    oneTap.delegate = self;
    oneTap.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:oneTap];

}
//- (void)back
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}

- (void)hideKeyBoard
{
    [self.userNameTextfield resignFirstResponder];
    [self.phoneNumTextfield resignFirstResponder];
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    return  YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.phoneNumTextfield resignFirstResponder];
    [self.userNameTextfield resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)nextButoon:(id)sender {
    if (![userNameTextfield.text length] || ![phoneNumTextfield.text length]) {
        [JddCommonFunction messageBoxWithMessage:NAME_PHNOE_NOT_ALLOWED_EMPTY];
        return;
    }

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^([1][3,4,5,8])([0-9]{9})$"];
    if (![phoneNumTextfield.text length] || ![pred evaluateWithObject:phoneNumTextfield.text]) {
        [JddCommonFunction messageBoxWithMessage:PLEASE_INPUT_MOBILE];
        return;
    }
    [JddCommonFunction setWaitingViewHidden:NO fromController:self];
    
    JddLookForPasswordOperation *operation = [[JddLookForPasswordOperation alloc] init];
    operation.delegate = self;
    operation.userName = userNameTextfield.text;
    operation.userPhoneNum = phoneNumTextfield.text;
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
}

- (IBAction)phoneBtnClick:(id)sender {
    
    //[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://4006998636"]];
    NSString *number = @"18674849832";
    NSString *num = [[NSString alloc] initWithFormat:@"telprompt://%@",number];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]]; //拨号
}

- (IBAction)phoneBtnClick2:(id)sender {
    NSString *number = @"18674899170";
    NSString *num = [[NSString alloc] initWithFormat:@"telprompt://%@",number];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]]; //拨号
}

- (void)FindPasswordOperationResult:(NSData *)result
{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:nil];
    
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        NSString *uId = [JddDataParser parseLoginResult:result];
        if (uId.length == 0) {
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
        }
        if ([[dic objectForKey:@"Code"]intValue] == 0) {
           [[JddViewControllerManager sharedInstance]pushViewControllerWithIndex:CONTROLLER_CONFIRM fromController:self withObject:userNameTextfield.text withObject2:phoneNumTextfield.text];
        }
    }
}


@end
